package com.three.vip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.UserAddress;
import com.three.core.vo.R;
import com.three.vip.mapper.UserAddressMapper;
import com.three.vip.service.UserAddressService;
import com.three.vip.vo.UserAddressFormVo;
import com.three.vip.vo.UserAddressSearchFormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：JinShiYan
 * @date ：Created in 2025/3/12 18:21
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public R<IPage<UserAddress>> selectByCondition(UserAddressSearchFormVo userAddressSearchFormVo) {
        Page<UserAddress> page = new Page<>(userAddressSearchFormVo.getCurrentPage(),
                userAddressSearchFormVo.getPageSize());
        IPage<UserAddress> userAddressIPage = userAddressMapper.selectByCondition(page,
                userAddressSearchFormVo.getAddressName());
        return new R<>(200, "查询成功", userAddressIPage);

    }

    @Override
    public R<String> updateAddress(UserAddressFormVo userAddressFormVo) {
        int result = userAddressMapper.updateAddress(userAddressFormVo);
        if (result > 0) {
            return new R<>(200, "更新成功", null);
        } else {
            return new R<>(400, "更新失败", null);
        }
    }
    //查询地址
    @Override
    public R<UserAddress> selectOneAddress(Integer addressId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_id", addressId);
        UserAddress userAddress = userAddressMapper.selectOne(queryWrapper);
        return new R<>(200, "查询成功", userAddress);
    }
    //添加地址
    @Override
    public R<UserAddress> addAddress(UserAddress userAddress) {
        int insert = userAddressMapper.insert(userAddress);
        if (insert > 0) {
            return new R<>(200, "添加成功", null);
        }
        return new R<>(400, "添加失败", null);
    }
    //修改地址
    @Override
    public R<UserAddress> updateUserAddress(UserAddress userAddress) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_id", userAddress.getAddressId());
        int update = userAddressMapper.update(userAddress, queryWrapper);
        if (update > 0) {
            return new R<>(200, "修改成功", null);
        }
        return new R<>(400, "修改失败", null);
    }
    // 删除地址
    @Override
    public R<String> deleteUserAddress(Integer addressId) {
        int i = userAddressMapper.deleteByMyId(addressId);
        if (i > 0) {
            return new R<>(200, "删除成功", null);
        }
        return new R<>(400, "删除失败", null);
    }
    //查询当前用户保存的所有地址信息
    @Override
    public R<List<UserAddress>> selectAddressById(Integer userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<UserAddress> userAddresses = userAddressMapper.selectList(queryWrapper);
        return new R<>(200, "查询成功", userAddresses);
    }
}
